package com.zbb.online_exam.controller;

import com.alibaba.fastjson.JSON;
import com.zbb.online_exam.bean.JwtOperation;
import com.zbb.online_exam.constant.Method;
import com.zbb.online_exam.dto.JsonResult;
import com.zbb.online_exam.dto.UserDto;
import com.zbb.online_exam.entity.Menu;
import com.zbb.online_exam.exception.ExamException;
import com.zbb.online_exam.mapper.MenuMapper;
import com.zbb.online_exam.service.MenuService;
import com.zbb.online_exam.service.UserService;
import com.zbb.online_exam.utils.ExamUtils;
import com.zbb.online_exam.utils.HttpUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zbbstart
 * @date 2021/1/30 - 21:21
 */
@RestController
@RequestMapping("menus")
@Api(tags = "考试系统相关功能的接口")
@Slf4j
public class MenuController {
    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private MenuService menuService;

    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_TEACHER','ROLE_STUDENT')")
    @GetMapping
    @ApiOperation(value = "获取所有的菜单功能", notes = "当用户登录到达主页面时就会请求该链接渲染出所有的菜单功能")
    public JsonResult getMenuList(@ApiParam(name = "token", example = "") @RequestHeader("auth-token") String token, HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            List<Menu> menuList = menuService.getMenuInfo(token);
            result = httpUtils.buildSuccess(Method.GET, menuList);
            log.info("菜单:"+result);
        }catch (ExamException e){
            result = httpUtils.buildFailure(e.getMessage(), Method.GET);
        }catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }
}
